import { defineConfig, loadEnv } from "vite";
import react from "@vitejs/plugin-react";
import path from "path";
import { viteMockServe } from "vite-plugin-mock";
import { createHtmlPlugin } from "vite-plugin-html";

import pkg from "./package.json";
import dayjs from "dayjs";
import config from "./src/config";
const { dependencies, devDependencies, name, version } = pkg;
const __APP_INFO__ = {
  pkg: { dependencies, devDependencies, name, version },
  lastBuildTime: dayjs().format("YYYY-MM-DD HH:mm:ss"),
};
// https://vitejs.dev/config/
export default defineConfig(({ command, mode }) => {
  const env = loadEnv(mode, process.cwd(), "");
  console.log(env.NODE_ENV);
  const isDev = env.NODE_ENV === "development";

  return {
    plugins: [
      react(),
      // mock配置
      viteMockServe({
        //配置mock位置
        mockPath: "mock",
        supportTs: false,
        localEnabled: isDev,
        prodEnabled: !isDev,
        watchFiles: true,
        injectCode: `
            import { setupProdMockServer } from './mockProdServer';
            setupProdMockServer();
          `,
        injectFile: path.resolve(process.cwd(), "src/main.jsx"),
      }),
      createHtmlPlugin({
        inject: {
          data: {
            title: config().title,
            version: config().version,
          },
        },
      }),
    ],
    define: {
      __APP_INFO__: JSON.stringify(__APP_INFO__),
    },
    resolve: {
      alias: {
        "@": path.resolve(__dirname, "./src"),
      },
    },
    css: {
      preprocessorOptions: {
        // postcss: {
        //   // 全局添加scss
        //   additionalData: `@use '@/global.less';`,
        // },
      },
    },
    build: {
      // 浏览器兼容性  “esnext | modules”
      target: "modules",
      outDir: "./dist",
      // 构建后是否生成 sourcemap 文件
      sourcemap: false,
      // 块大小警告大小限制(kb)
      chunkSizeWarningLimit: 2000,
      /** Vite 2.6.x 以上需要配置 minify: "terser", terserOptions 才能生效 */
      minify: "terser",
      /** 在打包代码时移除 console.log、debugger 和 注释 */
      terserOptions: {
        //打包后移除console和注释
        compress: {
          drop_console: false,
          drop_debugger: true,
          keep_infinity: true,
          pure_funcs: ["console.log"],
        },
        format: {
          /** 删除注释 */
          comments: false,
        },
      },

      /** 打包后静态资源目录 */
      assetsDir: "static",
      rollupOptions: {
        output: {
          // 分解大块js,
          manualChunks(id) {
            if (id.includes("node_modules")) {
              return id
                .toString()
                .split("node_modules/")[1]
                .split("/")[0]
                .toString();
            }
          },
          // 用于从入口点创建的块的打包输出格式[name]表示文件名,[hash]表示该文件内容hash值
          entryFileNames: "static/js/[name].[hash].js",
          // 用于命名代码拆分时创建的共享块的输出命名
          chunkFileNames: "static/js/[name].[hash].js",
          // 用于输出静态资源的命名，[ext]表示文件扩展名
          assetFileNames: "static/[ext]/[name].[hash].[ext]",
        },
      },
    },
    server: {
      hmr: true,
      // proxy: {
      //   "/api": {
      //     target: "http://192.168.3.25:8131/api",
      //     ws: false,
      //     changeOrigin: true,
      //     rewrite: (path) => path.replace(/^\/api/, ""),
      //   },
      // },
    },
  };
});
